﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace AngryNerds
{
    public abstract class GameObject : DrawableGameComponent, IPhysicalObject
    {
        protected Texture2D objTexture;
        protected Vector2 position;
        protected Vector2 origin;
        protected float zRotation;

        /// <summary>
        /// Number of frames. If frame is 1 game 
        /// object has single frame otherwise multiple frames to switch.
        /// </summary>
        protected int frameCount = 1;
        protected int currentFrame = 0;
        protected SpriteBatch spriteBatch;
        
        protected AngryGame AngryGame
        {
            get
            {
                return (AngryGame)Game;
            }
        }

        protected ContentManager Content
        {
            get
            {
                return AngryGame.Content;
            }
        }

        public Rectangle BoundingRectangle
        {
            get
            {
                return new Rectangle(0, objTexture.Height / frameCount,
                    objTexture.Width, objTexture.Height / frameCount);
            }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        /// <summary>
        /// Rotation over z-axis.
        /// </summary>
        public float ZRotation
        {
            get { return zRotation; }
            set { zRotation = value; }
        }

        /// <summary>
        /// Initializes game object for Angry Game and loads content.
        /// </summary>
        /// <param name="angryGame">Corresponding "Angry Game".</param>
        public GameObject(AngryGame angryGame)
            : base(angryGame)
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            LoadContent();
        }

        protected void DrawWithFrame()
        {
            spriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);

            spriteBatch.Draw(objTexture, position, BoundingRectangle, Color.White,
               zRotation, origin, 1.0f, SpriteEffects.None, 1.0f);

            spriteBatch.End();
        }

        #region IPhysicalObject Members

        public abstract void OnCollide();

        #endregion
    }
}
